<template>
  <view class="page">
    <uni-swipe-action>
      <view class="list" v-for="item in list" :key="item.id">
        <uni-swipe-action-item class="list-item">
          <template v-slot:left>
            <!-- 左侧操作可根据需要添加 -->
          </template>
          <view class="content-box" @click="handleEdit(item)">
            <image
              class="stroeListItem-pic"
              :src="formatImageUrl(item.showPic)"
              mode="aspectFill"
            ></image>
            <view class="stroeListItem-right">
              <view class="top">{{ item.name }}</view>
              <view class="bottom">
                <view class="money">￥{{ item.money }}</view>
              </view>
              <view class="address"
                >状态：{{ item.status === 1 ? "已下架" : "已上架" }}</view
              >
            </view>
          </view>
          <template v-slot:right>
            <view class="slot-button danger" @click="handleDelete(item)"
              ><text class="slot-button-text">删除</text></view
            >
            <view
              class="slot-button primary"
              v-if="item.status === 1"
              @click="handleOnOff(item, 0)"
              ><text class="slot-button-text">上架</text></view
            >
            <view
              class="slot-button warning"
              v-if="item.status === 0"
              @click="handleOnOff(item, 1)"
              ><text class="slot-button-text">下架</text></view
            >
            <view class="slot-button success" @click="handleEdit(item)"
              ><text class="slot-button-text">编辑商品</text></view
            >
          </template>
        </uni-swipe-action-item>
      </view>
    </uni-swipe-action>
    <view class="bank-btn" @click="goAddGoods()">添加商品</view>
  </view>
</template>

<script setup>
import { ref, getCurrentInstance } from "vue";
import { onShow } from "@dcloudio/uni-app";
import { formatImageUrl } from "@/utils/filters";
import { useEditorStore } from "@/store/modules/editor";
import { useUserStore } from "@/store/modules/user";
import { useStoreStore } from "@/store/modules/store";
import { getGoodsList, deleteGoods, goodsOnOff } from "@/api/goods";

// 获取实例用于访问全局属性
const instance = getCurrentInstance();
const $rsa = instance.proxy.$rsa;

// 使用 store
const editorStore = useEditorStore();
const userStore = useUserStore();
const storeStore = useStoreStore();

// 商品列表数据
const list = ref([]);
const page = ref(1);

// 获取商品列表
const getData = async () => {
  try {
    const res = await getGoodsList({
      userId: userStore.userInfo?.userId,
      merchantId: storeStore.storeId,
      limit: 100,
      offset: page.value,
    });

    if (res.data?.rows) {
      list.value = res.data.rows;
    }
  } catch (error) {
    console.error("获取商品列表失败:", error);
    uni.showToast({
      title: "获取商品列表失败",
      icon: "none",
    });
  }
};

// 跳转到添加商品页面
const goAddGoods = () => {
  uni.navigateTo({
    url: "./goodsDetail?type=add",
  });
};

// 编辑商品
const handleEdit = (item) => {
  uni.navigateTo({
    url: `./goodsDetail?type=edit&goodsId=${item.id}`,
  });
};

// 删除商品
const handleDelete = (item) => {
  uni.showModal({
    title: "提示",
    content: "确定删除此商品",
    success: async (res) => {
      if (res.confirm) {
        try {
          const response = await deleteGoods({
            userId: userStore.userInfo?.userId,
            merchantId: storeStore.storeId,
            id: item.id,
          });

          if (response.code === 2000) {
            uni.showToast({
              title: "删除成功",
              icon: "none",
            });
            getData();
          }
        } catch (error) {
          console.error("删除商品失败:", error);
          uni.showToast({
            title: "删除失败",
            icon: "none",
          });
        }
      }
    },
  });
};

// 商品上下架
const handleOnOff = (item, status) => {
  const msg = status === 0 ? "确定上架该商品" : "确定将该商品下架";

  uni.showModal({
    title: "提示",
    content: msg,
    success: async (res) => {
      if (res.confirm) {
        try {
          const response = await goodsOnOff({
            userId: userStore.userInfo?.userId,
            merchantId: storeStore.storeId,
            id: item.id,
            status: status,
          });

          if (response.code === 2000) {
            uni.showToast({
              title: "操作成功",
              icon: "none",
            });
            getData();
          }
        } catch (error) {
          console.error("操作失败:", error);
          uni.showToast({
            title: "操作失败",
            icon: "none",
          });
        }
      }
    },
  });
};

// 页面显示时
onShow(() => {
  // 初始化用户信息
  userStore.hydrateFromStorage();
  storeStore.hydrateFromStorage();
  // 清空富文本编辑器内容
  editorStore.clearRichText();
  // 获取商品列表
  getData();
});
</script>

<style lang="scss">
.page {
  display: flex;
  flex-direction: column;
  padding-bottom: 150rpx;

  .list {
    .list-item {
      margin-top: 20rpx;

      .content-box {
        width: 100%;
        background-color: #ffffff;
        color: #000000;
        font-size: 14px;
        display: flex;
        justify-content: space-between;
        padding: 30rpx;

        .stroeListItem-pic {
          width: 200rpx;
          height: 200rpx;
          border-radius: 10rpx;
        }

        .stroeListItem-right {
          width: calc(100% - 240rpx);

          .top {
            font-size: 30rpx;
            font-weight: 600;
            display: -webkit-box;
            overflow: hidden;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
          }

          .bottom {
            display: flex;
            width: 100%;
            align-items: center;
            padding: 20rpx 0;

            .money {
              color: rgba(236, 65, 19, 1);
              font-size: 28rpx;
            }

            .btn {
              color: rgba(171, 168, 168, 0.88);
              font-size: 24rpx;
              text-decoration: line-through;
              margin-left: 2px;
            }
          }

          .address {
            font-size: 30rpx;
            font-weight: 600;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
            color: #ababab;
          }

          .look {
            background-color: #ffebd8;
            color: #e08e40;
            display: inline-block;
            margin-top: 40rpx;
            padding: 4rpx 10rpx;
          }
        }
      }

      .slot-button {
        /* #ifndef APP-NVUE */
        display: flex;
        height: 100%;
        /* #endif */
        flex: 1;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        padding: 0 20px;

        .slot-button-text {
          color: #ffffff;
          font-size: 14px;
        }
      }

      .warning {
        background-color: #e6a23c;
      }

      .primary {
        background-color: #409eff;
      }

      .success {
        background-color: #67c23a;
      }

      .danger {
        background-color: #f56c6c;
      }
    }
  }

  .bank-btn {
    position: fixed;
    bottom: 8px;
    z-index: 95;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 80vw;
    height: 40px;
    font-size: 16px;
    color: #fff;
    background-color: $base-color;
    border-radius: 5px;
    box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
    left: 50%;
    transform: translate(-50%);
  }
}

.content-text {
  font-size: 15px;
}

.example-body {
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  flex-direction: row;
  justify-content: center;
  padding: 10px 0;
  background-color: #fff;
}

.button {
  border-color: #e5e5e5;
  border-style: solid;
  border-width: 1px;
  padding: 4px 8px;
  border-radius: 4px;
}

.button-text {
  font-size: 15px;
}
</style>
